﻿
using System ;

using MskEdit.Sdk ;
using MskEdit.Dxf.Representation ;


namespace MskEdit.Dxf.Model
{
    public class DxfEntity : DxfStructBase
    {

        public static DxfEntity TryFrom( string[] allLines, ref int lineNo, DxfEntity fallbackValue ) {
            
            if ( allLines.Length <= lineNo + 1 ) 
                return fallbackValue ;
        
            int internalLineNo = lineNo + 2 ;
                
            if ( ! DxfValidEntityNames.contains( allLines[internalLineNo - 1] ) )
                return fallbackValue ;
            
            bool endEntityFound = false ;
            
            for ( ; internalLineNo < allLines.Length; internalLineNo += 2 ) {
                endEntityFound = "0".Equals( allLines[internalLineNo] ) ;
                if ( endEntityFound )
                    break ;
            }
            
            if ( ! endEntityFound )
                throw new SdkException( "End of entity not found in dxf lines." ) ;
            
            SdkSubrange subrange = new SdkSubrange( lineNo, internalLineNo - lineNo ) ;
                
            lineNo = subrange.End ;
            
            return new DxfEntity( allLines, subrange ) ;
        }


        public DxfEntity( string[] allLines, SdkSubrange subrange ) : base( allLines, subrange ) {
        }
        
    }
}
